<?php

namespace App\Models;

use App\Events\ManagerMoneyChangeEvent;
use App\Models\Pay as PayModel;
use App\Notifications\ResetPasswordNotification;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password','alipay_account'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token','api_token'
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function sendPasswordResetNotification($token)
    {
        $this->notify(new ResetPasswordNotification($token));
    }

    public function parent(){
        if ($this->pid>0){
            return User::find($this->pid);
        }else{
            return null;
        }
    }


    public function servers()
    {
        return $this->hasMany('App\Models\Server','user_id','id')->orderBy('created_at','desc');
    }
    public function ads()
    {
        return $this->hasMany('App\Models\Advert','user_id','id')->orderBy('created_at','desc');
    }

    public function serverAuths()
    {
        return $this->hasMany('App\Models\ServerAuth','user_id','id')->orderBy('created_at','desc')->with('serverEngine');
    }

    public function singleAuths()
    {
        return $this->hasMany('App\Models\AuthSingle','user_id','id')->orderBy('created_at','desc');
    }

    public function generateToken(){
//        $token=hash('sha256',random_bytes(32));
        $this->api_token= 'awd4123fa4231dawe';
        $this->save();
    }


    public function Recharge(PayModel $pay,bool $isPlayerRecharge=false){

        //客户充值
        $money = $pay->money * config('conf.oneRMBToMoney');

        $msg='';
        if ($isPlayerRecharge){//玩家充值
            $server = Server::find($pay->server_id);
            if ($server->user_id_cooperation==0){
                //无合作人
                $this->remain_money += $money;
                $this->save();
                event(new ManagerMoneyChangeEvent($pay->manager_id,$pay->server_id, $pay->player_id,3,1,$money,0,$this->remain_money,'玩家充值'.($pay->payway==1?'(微信)':'(支付宝)'),$pay->order_num));
            }else{
                //
                $money1=$money*($server->cooperation_ratio/100);
                $money2=$money-$money1;
                if ($money2>0){
                    $this->remain_money += $money2;
                    $this->save();
                    event(new ManagerMoneyChangeEvent($pay->manager_id,$pay->server_id, $pay->player_id,3,1,$money2,0,$this->remain_money,"玩家充值分成".($pay->payway==1?'(微信)':'(支付宝)'),$pay->order_num));
                }
                $uCoop=User::find($server->user_id_cooperation);
                if ($uCoop && $money1>0){
                    $uCoop->remain_money += $money1;
                    $uCoop->save();
                    event(new ManagerMoneyChangeEvent($uCoop->id,$pay->server_id, $pay->player_id,3,1,$money1,0,$uCoop->remain_money,"玩家充值分成".($pay->payway==1?'(微信)':'(支付宝)'),$pay->order_num));
                }
            }
        }else{
            $this->remain_money += $money;
            $this->save();
            event(new ManagerMoneyChangeEvent($pay->manager_id,$pay->server_id, $pay->player_id,4,1,$money,0,$this->remain_money,'客户充值'.($pay->payway==1?'(微信)':'(支付宝)'),$pay->order_num));
        }

        //推荐人奖励
        $parent=$this->parent();
        if ($parent){
            $parent->remain_money += $money*0.05;
            $parent->save();
            if ($isPlayerRecharge){
                event(new ManagerMoneyChangeEvent($parent->id,$pay->server_id, $pay->player_id,6,1,$money*0.05,0,$parent->remain_money,'推荐客户的玩家充值奖励',$pay->order_num));
            }else{
                event(new ManagerMoneyChangeEvent($parent->id,$pay->server_id, $pay->player_id,5,1,$money*0.05,0,$parent->remain_money,'推荐客户充值奖励',$pay->order_num));
            }

        }
    }
}
